package com.xj.hk.service.impl;

import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.xj.hk.dao.SpDao;
import com.xj.hk.entity.Sp;
import com.xj.hk.service.SpService;
import com.xj.util.Page;

@Component
@Transactional
public class SpServiceImpl implements SpService {

	@Autowired
	private SpDao spDao;
	

	@Override
	public void save(Sp sp) {
		spDao.saveOrUpdate(sp);
		spDao.flush();
	}
	

	@Override
	public Page<Sp> queryByPage(Map<String, Object> map, int pageNo, int pageSize, String orderBy, String order) {
		Page<Sp> page = new Page<Sp>();
		if(orderBy!=null && order!=null)
		page.pageNo(pageNo).pageSize(pageSize).autoCount(true).orderBy(orderBy).order(order);
		else page.pageNo(pageNo).pageSize(pageSize).autoCount(true);
		return spDao.findPage(page, map);
	}


	@Override
	public void delete(int id) {
		String hql="delete Sp where id=?";
		Query query  = spDao.getSession().createQuery(hql).setParameter(0,id);
		query.executeUpdate();
		spDao.flush();
	}


	@Override
	public Sp getEntity(int id) {
		String hql="from Sp where id=?";
		Query query = spDao.getSession().createQuery(hql).setParameter(0,id);
		 List list = query.list();
        if (list == null||list.size()<1) {
            return null;
        }
	    return (Sp) list.get(0);
	}

}
